package com.sfac.javaSpringBoot.modules.common.service.impl;

import com.sfac.javaSpringBoot.modules.common.dao.ExceptionLogDao;
import com.sfac.javaSpringBoot.modules.common.entity.ExceptionLog;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.service.ExceptionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {
    @Autowired
    private ExceptionLogDao exceptionLogDao;
    @Override
    @Transactional
    public Result<ExceptionLog> insertExceptionLog(ExceptionLog exceptionLog) {
       exceptionLog.setCreateDate(LocalDateTime.now());
       List<ExceptionLog> exceptionLogs =
               Optional.ofNullable(
                       exceptionLogDao.getExceptionLogsByParam(
                               exceptionLog.getPath(),
                               exceptionLog.getMethodName(),
                               exceptionLog.getExceptionType()
                       )
               ).orElse(Collections.emptyList());
       if (exceptionLogs.size()==0){
           exceptionLogDao.insertExceptionLog(exceptionLog);
       }

        return new Result<>(Result.ResultCode.SUCCESS.code,
                "insert success");
    }
}
